import {
	request
} from '@/util/ajax.js'
let getyhtUserId = function(param) {
	Promise.all([getToken(),getUser()]).then(tokenArr => {
		getId(tokenArr[0].data.access_token, tokenArr[1].OAuthCode).then(res => {
			request({
				methods: 'GET',
				url: "/hgjt-be/mobile/ysx/getUserById?userId="+res.data.data.yhtUserId,
			}).then(res => {
				sessionStorage.setItem('xyyluserid',JSON.stringify(res.data))
			})
			 
		})

	}).catch(err => {
		//csonsole.log(err)
		alert('err = ' + JSON.stringify(err))
	})

}
function getUser(params) {
    return new Promise((reslove, reject) => {
        YYEsnBridge.ready(function () {
            YYEsnBridge.do('getOAuthCode', {
                'success': function (res) {
                    reslove(res);
                    // alert(JSON.stringify(res))
                    // /alert('调用成功');
                },
                'fail': function (err) {
                    reject(err)
                },
                // 'complete': function(res){alert('调用完成后执行');}
            });
        });
    })
}
function getToken(params) {
	return new Promise((reslove, reject) => {
		request({
			methods: 'GET',
			//url: "/hgjt-be/userInfo/getBipToken?appKey=ed451b33596d4accb410df146efd98a3&appSecret=dc55c3cb587e46e1bf24ae9da2ddd143",//测试
			url: "/hgjt-be/userInfo/getBipToken?appKey=efc77a1be2054b42be355fd2bde6ff8d&appSecret=b414eddc974c4b09845d85d4f2f12dea",//正式
		}).then(res => {
			 // alert(JSON.stringify(res))
			console.log(res)
			reslove(res)
		})
	})

}
function getId(access_token, code) {
	return new Promise((reslove, reject) => {
		request({
			methods: 'GET',
			url: "/iuap-api-gateway/yonbip/uspace/freelogin/base_info_by_code?access_token="+ access_token +"&code=" +code,
		}).then(res => {
			 // alert(JSON.stringify(res))
			reslove(res)
		})
	})
}
export {
	getyhtUserId
};
